<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>快速排序</title>
        <script type="text/javascript" src="jquery-1.7.2.min.js"></script>
    </head>
    <body> 
    <script type="text/javascript">
    var n=0;//次数
  var arr=[];
     function swap(arr,i,j) {
        var c = arr[i];
        arr[i] = arr[j];
        arr[j] = c;
    }
    function quickSort(arr,first,end){
        if(first>=end){
            return arr;
        }
        var tmp=arr[first];
        var i=first,j=end;
        while(i<j){
            while (arr[j] >= tmp && i < j) {
                j--;
            }
            while (arr[i] <= tmp && i < j) {
                i++;
            }
            swap(arr,i,j);
        }
        swap(arr,first,j); 
        n++;
        $("#result").append("第"+n+"次排序结果"+arr+"<br/>");
        quickSort(arr,first,j-1);  
        quickSort(arr,j+1,end);
    }
    function getvalue(arr2){
        for(var i=0;i<arr2.length;i++)
        arr[i]=parseInt(arr2[i]);
        return arr;
    }
    function quickSort1()
    {  var arr2=document.getElementById("text1").value.split(",");
       getvalue(arr2);
       document.getElementById('get1').innerHTML="排序前" + arr;
       quickSort(arr,0,arr.length-1);
       document.getElementById("get").innerHTML="最终排序结果:" + arr;
    }
    </script>
    <h1>快速排序</h1>
    <p>输入一个数组：(例如：21,5,45,12,56,8)</p>
    <input type="text" id="text1" name="text1" size="50px"  maxlength="50px" value="" >
        <input type="button"  value="quickSort" onclick="quickSort1()"/>
        <p id="get1"> </p>
        <p id="result"> </p>
        <p id="get"> </p>
    </body>
</html>